export const setTime = function (time: string) {
    let minutes = Math.floor(Number(time) / 60000);
    let seconds = Math.floor((Number(time) - minutes * 60000) / 1000)
    if (seconds < 10) {
        return `${minutes}:0${seconds}`
    } else
        return `${minutes}:${seconds}`
}


export const setIntrduces = function (item: any) {
    if (typeof (item) == "string") {
        if (item == "") {
            return item = [];
        }
        else {
            item = item.replace(/\t/g, '')
            return item.split(/[\n]/)
        }
    } else {
        return item = [];
    }
}
export const setCommentTime = function (item: any) {
    if (typeof (item) == "string") {
        if (item == "") {
            return item = [];
        }
        else {
            item = item.replace(/T/g, ' ')
            return item.split(/[.]/)
        }
    } else {
        return item = [];
    }
}

export const setYear = function (time: number) {
    if (typeof time === 'undefined') return 0
    const t = new Date(time)
    let year = t.getFullYear()
    let mouth = t.getMonth() + 1
    let day = t.getDay()
    if (mouth && year) {
        mouth = mouth < 10 ? Number('0' + mouth) : Number(mouth)
        day = day < 10 ? Number('0' + day) : Number(day)
    }
    return year + '-' + mouth + '-' + day
}

export const formatLyric = (lyric?: string): IReturnLyric => {
    if (lyric === '') {
        return { lyric: [{ time: 0, lyric: '该歌曲暂无歌词', uid: 2333 }] }
    }
    const lyricObjArr: ILyric[] = []
    const lineLyric: any = lyric?.split(/\n/)
    const regTime = /\d{2}:\d{2}.\d{2,3}/
    for (let i = 0; i < lineLyric?.length; i++) {
        if (lineLyric[i] === '') continue
        const time: number = formatLyricTime(lineLyric[i].match(regTime)[0])

        if (lineLyric[i].split(']')[1] !== '') {
            lyricObjArr.push({
                time: time,
                lyric: lineLyric[i].split(']')[1],
                uid: parseInt(Math.random().toString().slice(-6))
            })
        }
    }
    return {
        lyric: lyricObjArr
    }
}

const formatLyricTime = (time: string) => {
    const regMin = /.*:/
    const regSec = /:.*\./
    const regMs = /\./

    const min = parseInt((time.match(regMin) as any)[0].slice(0, 2))
    let sec = parseInt((time.match(regSec) as any)[0].slice(1, 3))
    const ms = time.slice((time.match(regMs) as any).index + 1, (time.match(regMs) as any).index + 3)
    if (min !== 0) {
        sec += min * 60
    }
    return Number(sec + '.' + ms)
}
export interface ILyric {
    time: number,
    lyric: string,
    uid: number
}
interface IReturnLyric {
    lyric: ILyric[],
}